package com.example.automanagement.controller;

import com.example.automanagement.entity.*;
import com.example.automanagement.service.ApplicationService;
import com.example.automanagement.util.Result;
import com.example.automanagement.views.DepartmentRole;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

@RestController
public class ApplicationController {
    @Resource
    private ApplicationService applicationService;
    @PostMapping(value = "/addAutoOrder")
    public Result addAutoOrder(@RequestBody AutoOrder autoOrder){
        applicationService.addAutoOrder(autoOrder);
        return Result.success();
    }
    @PostMapping(value = "/addRepairOrder")
    public Result addRepairOrder(@RequestBody RepairOrder repairOrder){
        applicationService.addRepairOrder(repairOrder);
        return Result.success();
    }

    //TODO
    // 1.找到所对应的部门管理员的工号
    // 2.生成对应角色的代办 ，工单进度置1
    @GetMapping("/queryDepartmentUserId")
    public Result queryDepartmentUserId(@RequestParam("userRole") String userRole, @RequestParam("userDepartment") String userDepartment){
        if(userRole != null && userDepartment != null) {
            DepartmentRole role  = applicationService.getUserIdByDR(userRole, userDepartment);
            return Result.success(role);
        }
        return Result.error("500","错误");
    }
    @PostMapping("/insertTodo")
    public Result insertTodo(@RequestBody ToDo toDo){
        if(toDo == null || toDo.getOrderId() == null)
            return Result.error("500","检查输入数据");
        applicationService.addTodo(toDo);

        return Result.success(toDo);

    }
    @GetMapping("/getAutoOrderId")
    public Result getAutoOrderId(@RequestParam("submitTime") String submitTime){
        if(submitTime == null)
            return Result.error("500", "错误");
        AutoOrder autoOrder = applicationService.getAutoOrderIdByTime(submitTime);
        return Result.success(autoOrder);
    }
    @GetMapping("/getRepairOrderId")
    public Result getRepairOrderId(@RequestParam("submitTime") String submitTime){
        if(submitTime == null)
            return Result.error("500", "错误");
        RepairOrder repairOrder = applicationService.getRepairOrderIdByTime(submitTime);
        return Result.success(repairOrder);
    }
    @GetMapping("/getGeneralUserId")
    public Result getGeneralUserId(){
        GeneralRole role  = applicationService.getUserIdByGn();
        return Result.success(role);
    }
    //修改待办状态为已处理
    @PutMapping("/updateTodoStatus")
    public Result updateTodoStatus(@RequestParam("todoType") String todoType,@RequestParam("orderId") String orderId){
        if(todoType == null || orderId == null)
            return Result.error("500", "非法输入");
        applicationService.updateTodoDone(todoType, orderId);
        return Result.success();
    }
    //更新用车工单状态
    @PutMapping("/updateAutoOrderProgress")
    public Result updateAutoOrderProgress(@RequestParam("orderId") String orderId,@RequestParam("autoOrderProgress") String autoOrderProgress){
        if(orderId == null || autoOrderProgress == null)
            return Result.error("500", "非法输入");
        applicationService.updateAutoOrderProgress(orderId,autoOrderProgress);
        return Result.success();
    }
    //更新维修工单状态
    @PutMapping("/updateRepairOrderProgress")
    public Result updateRepairOrderProgress(@RequestParam("orderId") String orderId,@RequestParam("repairOrderProgress") String repairOrderProgress){
        if(orderId == null || repairOrderProgress == null)
            return Result.error("500", "非法输入");
        applicationService.updateRepairOrderProgress(orderId,repairOrderProgress);
        return Result.success();
    }
}
